/* jshint esversion: 6 */
// User 登陆接口
function User(Site, call) {
    var $ = this;
    this.Key = "";
    this.uuid = "";
    this.Site = Site;
    this.UserStatus = false;
    this.Initialize = false;
    // 心跳包处理
    setInterval(function () {
        if ($.UserStatus) {
            $.Post($.Site + "/UserHeartBeat", {
                Key: $.Key
            });
        }
        if ($.initialize) {
            $.Post($.Site + "/CacheHeartBeat", {
                Key: $.uuid
            });
        }
    }, 300000); //5分钟一个心跳包
    // 得到 uuid
    $.Post($.Site + "/GetKey", {}, function (e, data) {
        if (e != undefined) {
            call("Key获取失败");
            return;
        }
        $.uuid = data.Key;
        $.initialize = true;
        if (call) {
            call(null);
        }
    });
    // Login 登陆
    this.Login = function (User, Password, call) {
        $.Post($.Site + "/Login", {
            Key: $.uuid,
            User: User,
            Password: md5($.uuid + md5(Password))
        }, function (error, data) {
            if (error != undefined){
                call(error, null);
                return;
            }
            if (data.Error != "") {
                if (call != undefined) {
                    call(data.Error, null);
                }
            } else {
                $.Key = data.Key;
                $.UserStatus = true;
                if (call != undefined) {
                    call(null, "登陆成功");
                }
            }
        });
    };
    // LoggedOut 退出
    this.LoggedOut = function () {
        $.Post($.Site + "/LoggedOut", {
            Key: $.Key
        });
        $.Key = "";
        $.UserStatus = false;
    };
}
// Post Post通信
User.prototype.Post = function (url, data, call) {
    PetUI.HTTP.Post({
        url: url,
        data: data
    }, function (error, data) {
        if (error != null) {
            if (call != undefined) {
                call(error, null);
            }
        } else {
            if (call != undefined) {
                call(null, data);
            }
        }
    });
};